/**
 * Created by blatand on 2017/2/9.
 */
public class ToolUtil {

    public static int DoubleToOne(int data){

        int temp = data;
        if(temp>=0x30&&temp<=0x39){
            temp = temp - 0x30 ;
        } else if(temp>=0x41&&temp<=0x46){
            temp = temp - 0x37;
        }else if(temp>=0x61&&temp<=0x66){
            temp = temp - 0x57;
        }else{
            temp = 0x00;
        }
        return temp;
    }

    public static byte[] sdkAscToBcd(byte[] data){

        int i = 0;
        int high,low,temp = 0;
        byte[] lpOut = new byte[data.length/2];
        for(i=0;i<data.length/2;i++){

            high = DoubleToOne(data[2*i]);
            low = DoubleToOne(data[2*i+1]);
            temp = ((high<<4)|low);
            lpOut[i] = (byte)temp;
        }
        return lpOut;
    }
    public static int bytesToInt(byte src){

        int s = 0;
        StringBuilder stringBuilder = new StringBuilder("");
        int v = src & 0xFF;
        String hv = Integer.toHexString(v);
        if (hv.length() < 2) {
            stringBuilder.append(0);
        }
        stringBuilder.append(hv);
        s = Integer.parseInt(stringBuilder.toString());
        return s;
    }
    public static void TraceHexString(int num,byte[] showStr,int len){
        int i=0;

        System.out.printf("[F%02d][L%04d][VALUE:",num,len);
        for(i=0;i<len;i++){
            System.out.printf("%02x ",showStr[i]);
        }
        System.out.printf("]\n");

    }
    public static String getHexString(int num,byte[] showStr,int len){

        String str = new String();
        str += String.format("[F%03d][L%04d][VALUE:",num,len);
        for(int i=0;i<len;i++){
            str += String.format("%02x ",showStr[i]);
        }
        str += "]\n";
        return str;
    }
    public static byte[] Int2Hex(int len,int type){

        byte[] lenght  = new byte[2];

        if(type==Msg8583Util.TYPE_BCDLLLVAR||type==Msg8583Util.TYPE_BCDLLVAR){

            lenght[0] = Byte.parseByte(Integer.toString(len));

        }else{
            lenght[0] = Byte.parseByte(Integer.toString(len/256));
            lenght[1] = Byte.parseByte(Integer.toString(len%256));
        }

        return lenght;
    }
}
